#include "delay_ms.h"

void delay_init() 
{

	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	//  HCLK/8
	fac_us=SystemCoreClock/8000000;				//1/8  
	fac_ms=(u16)fac_us*1000;					// 1 ms need number of systick  

}	

void delay_ms(u16 n_ms)
{	 		  	  
	u32 temp;		   
	SysTick->LOAD=(u32)nms*fac_ms;				//(SysTick->LOAD 24bit)
	SysTick->VAL =0x00;							 
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;	   
	do
	{
		temp=SysTick->CTRL;
	}while((temp&0x01)&&!(temp&(1<<16)));		      
	SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;	 
	SysTick->VAL =0X00;       					 	  	    
} 
